package com.antler.smt.portal.controller;

import com.antler.smt.entity.MemberEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.annotation.OperateCut;
import com.antler.smt.portal.mapper.MemberEntityMapper;
import com.antler.smt.portal.mapper.MemberNameInfoMapper;
import com.antler.smt.response.base.Result;
import com.antler.smt.utils.LogUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 自动生成会员昵称
 *
 * @author zhangyi
 * @date 2018/5/16 14:55
 */
@RestController
@RequestMapping("/builaName")
@Api(description = "采购商和服务商自动生成昵称")
public class BuildNickNameController extends BaseController {

    /**
     * 日志
     */
    private static Logger log = LogUtil.get();


    @Resource
    private MemberNameInfoMapper memberNameInfoMapper;

    @Resource
    private MemberEntityMapper memberEntityMapper;

    /**
     * 自动生成昵称
     *
     *
     * @return
     */
    @PostMapping("/createName")
    @ApiOperation(value = "自动生成昵称", httpMethod = "POST")
    @OperateCut(operationContent = "用户自动生成昵称")
    public Result createName(){

        Result result = null;
//            判断是否是第一次
            MemberEntity memberEntity1 = memberEntityMapper.selectByPrimaryKey(currentMember.getId());
            if(!memberEntity1.getNickName().equalsIgnoreCase("")){
                result = new Result(ResultCodeEnum.OPERATION_SUCCESS,"该用户昵称已生成");
            }else {
                //昵称生成规则
                Integer type = memberNameInfoMapper.getType(currentMember.getId());
                Integer count = memberNameInfoMapper.getCount(type)+1;
                String nickName = null;
                if (type.equals(1)) {
                    nickName = "易树加工厂" + count + "号";
                } else if (type.equals(2)) {
                    nickName = "易树采购商" + count + "号";
                }else if(type.equals(3) || type.equals(4)){
                    nickName = "共享工程师" + count + "号";
                }else if(type.equals(5)){
                    nickName = "易树采购员" + count + "号";
                }
                MemberEntity memberEntity = new MemberEntity();
                memberEntity.setId(currentMember.getId());
                memberEntity.setNickName(nickName);
                int i = memberNameInfoMapper.updateNickName(memberEntity);
                if (0 == i) {
                    result = new Result(ResultCodeEnum.APPEND_DATA_ERROR);
                } else {
                    result = new Result(ResultCodeEnum.OPERATION_SUCCESS);
                }
            }
        return result;
    }

}
